package com.vasthu.core.dao;

import com.vasthu.api.dao.CategoryDAO;
import com.vasthu.api.domain.Category;
import java.util.*;
import java.sql.*;

public class CategoryDAOImpl implements CategoryDAO {

	public boolean insertCategory(Connection con, Category cat) {
		if (cat == null)
			return false;
			
		PreparedStatement pst = null;
		
		try {
			pst = con.prepareStatement("INSERT INTO Category(cat_name, cat_desc, status) VALUES(?, ?, 1)");
			
            pst.setString(1, cat.getCategoryName());
            pst.setString(2, cat.getCategoryDescription());

            pst.executeUpdate();
			
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryDAOImpl->insertCategory()");
		}
		
		return true;
	}

	public Category getCategoryById(Connection con, long catId) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Category cat = new Category();
		
		try {
			pst = con.prepareStatement("SELECT * FROM Category WHERE cat_id = ? AND status = 1");
			pst.setLong(1, catId);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				cat.setCategoryId( rs.getInt("cat_id") );
				cat.setCategoryName( rs.getString("cat_name") );
				cat.setCategoryDescription( rs.getString("cat_desc") );
				break;
            }
			
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryDAOImpl->deleteCategoryById()");
		}
		
		return cat;
	}
	
	public Category getCategoryByName(Connection con, String catName) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Category cat = new Category();
		
		try {
			pst = con.prepareStatement("SELECT * FROM Category WHERE cat_name = ? AND status = 1");
			pst.setString(1, catName);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				cat.setCategoryId( rs.getInt("cat_id") );
				cat.setCategoryName( rs.getString("cat_name") );
				cat.setCategoryDescription( rs.getString("cat_desc") );
				break;
            }
			
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryDAOImpl->deleteCategoryByName()");
		}
		
		return cat;
	}
	
	public boolean deleteCategoryById(Connection con, long catId) {
		PreparedStatement pst = null;

		try {
			pst = con.prepareStatement("DELETE FROM Category WHERE cat_id = ?");
			pst.setLong(1, catId);
			pst.executeUpdate();
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryDAOImpl->deleteCategoryById()");
		}
		
		return true;
	}
    
    public List<Category> getAllCategorys(Connection con) {
		List<Category> catList = new ArrayList<Category>();
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		try {
            pst = con.prepareStatement("SELECT * FROM Category WHERE status = 1");
            rs = pst.executeQuery();

			while (rs.next()) {
				Category cat = new Category();
				
				cat.setCategoryId( rs.getInt("cat_id") );
				cat.setCategoryName( rs.getString("cat_name") );
				cat.setCategoryDescription( rs.getString("cat_desc") );
				
				catList.add(cat);
            }

        } catch (SQLException ex) {
            System.out.println("ERROR : UserDAOImpl->getAllUsers()");
        }
		
		return catList;
	}
}